package cn.com.kh.lark;

import com.lark.oapi.Client;
import com.lark.oapi.core.request.RequestOptions;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.im.v1.model.CreateMessageReq;
import com.lark.oapi.service.im.v1.model.CreateMessageReqBody;
import com.lark.oapi.service.im.v1.model.CreateMessageResp;

/**
 * @projectName: ngsapitask-hst
 * @package: cn.com.kh.lark
 * @className: sdkSendMSG
 * @description: TODO
 * @author: LiJT
 * @date: 2023-09-27 16:51
 * @version: 1.0
 */

// SDK 使用文档：https://github.com/larksuite/oapi-sdk-java/tree/v2_main
// 开发者复制该Demo后，需要修改Demo里面的"appID", "appSecret"为自己应用的appId,appSecret.
public class CreateMessageSample {

    public static void main(String arg[]) throws Exception {
        // 构建client
        Client client = Client.newBuilder("cli_a580097ffb30500c", "73vEFy5s8SG3RtfK8huocfCQPSYOVQQb").disableTokenCache() //如需SDK自动管理租户Token的获取与刷新,可删除该行
                .build();

        // 创建请求对象
        CreateMessageReq req = CreateMessageReq.newBuilder().receiveIdType("email").createMessageReqBody(CreateMessageReqBody.newBuilder().receiveId("lijt@kuangheng.com").msgType("text").content("{\"text\":\"test content\"}").uuid("").build()).build();

        // 发起请求
        // 如开启了Sdk的token管理功能，就无需调用 RequestOptions.newBuilder().tenantAccessToken("t-xxx").build()来设置租户token了
        CreateMessageResp resp = client.im().message().create(req, RequestOptions.newBuilder().tenantAccessToken("t-g1049rgMS5K36ESLNZIJQRD33MYYDRMSHSNRCMBK").build());

        // 处理服务端错误
        if (!resp.success()) {
            System.out.println(String.format("code:%s,msg:%s,reqId:%s", resp.getCode(), resp.getMsg(), resp.getRequestId()));
            return;
        }

        // 业务数据处理
        System.out.println(Jsons.DEFAULT.toJson(resp.getData()));
    }
}
